package com.tianlaiedu.service.impl;

import com.tianlaiedu.bean.Post;
import com.tianlaiedu.bean.ResultVO;
import com.tianlaiedu.dao.PostMapper;
import com.tianlaiedu.dao.RelationMapper;
import com.tianlaiedu.service.PostService;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;

/**
 * @author:hejing
 * @date:2021/12/1
 */
public class PostServiceImpl implements PostService {
    @Override
    public int addPost(Post post) {
        try (SqlSession sqlSession = com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)) {
            final PostMapper postMapper = sqlSession.getMapper(PostMapper.class);
            return postMapper.addPost(post);
        }
    }

    @Override
    public int delPost(Post post) {
        return 0;
    }

    @Override
    public int updPost(int id, Post post) {
        return 0;
    }

    @Override
    public Post finByid(int id) {
        try (SqlSession sqlSession = com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)) {
            final PostMapper postMapper = sqlSession.getMapper(PostMapper.class);
            return postMapper.finByid(id);
        }
    }

    @Override
    public Post finByname(String name) {
        try (SqlSession sqlSession = com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)) {
            final PostMapper postMapper = sqlSession.getMapper(PostMapper.class);
            return postMapper.finByname(name);
        }
    }

    @Override
    public List<Post> findBylabel(int id) {
        try (SqlSession sqlSession = com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)) {
            final PostMapper postMapper = sqlSession.getMapper(PostMapper.class);
            return postMapper.findBylabel(id);
        }
    }

    @Override
    public List<Post> findAll() {
        try (SqlSession sqlSession = com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)) {
            final PostMapper postMapper = sqlSession.getMapper(PostMapper.class);
            return postMapper.findAll();
        }
    }
    public Integer findApage() {
        try (SqlSession sqlSession = com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)) {
            final PostMapper postMapper = sqlSession.getMapper(PostMapper.class);
            return postMapper.findPage();
        }
    }

    @Override
    public ResultVO findPage(HashMap<String, Object> param) {
        // 处理分页数据
        // 先查询总条数
        Integer total = findApage();
        // 获取当前每页显示的条数
        int limit = (int) param.get("limit");
        // 计算当前的总页数
        int pageTotal = (int)Math.ceil(total*1.0 / limit);  // 6/5 = 1.2
        // 获取当前页显示的商品数据
        PostServiceImpl postService = new PostServiceImpl();
        List<com.tianlaiedu.bean.Post> all = postService.findAll();

        // 创建一个map集合，封装数据
        // 也可以自定义一个返回值对象 VO   valueObject
        return new ResultVO(total, pageTotal, all, 0, "");
    }

    @Override
    public Integer addPraise(int id) {
        try (SqlSession sqlSession = com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)) {
            final PostMapper postMapper = sqlSession.getMapper(PostMapper.class);
            return postMapper.addPraise(id);
        }
    }

    @Override
    public Integer delPraise(int id) {
        try (SqlSession sqlSession = com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)) {
            final PostMapper postMapper = sqlSession.getMapper(PostMapper.class);
            return postMapper.delPraise(id);
        }
    }

    @Override
    public List<Post> finTabel(String tabel) {
        try (SqlSession sqlSession = com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)) {
            final PostMapper postMapper = sqlSession.getMapper(PostMapper.class);
            return postMapper.finTabel(tabel);
        }
    }


}
